
<template>
  <div class="home-view" style="margin-top: 560px">
    <!-- 搜索区域 -->
    <div class="search-section">
      <div class="search-container">
        <div class="search-box">
          <a-input-search
            v-model:value="searchValue"
            placeholder="搜索商品、品牌、店铺..."
            size="large"
            @search="handleSearch"
            @focus="handleSearchFocus"
            @blur="handleSearchBlur"
            class="search-input"
          >
            <template #prefix>
              <SearchOutlined class="search-icon" />
            </template>
          </a-input-search>

          <!-- 搜索建议 -->
          <div v-if="showSearchSuggestions && (searchSuggestions.length > 0 || searchHistory.length > 0)" class="search-suggestions">
            <!-- 搜索历史 -->
            <div v-if="searchHistory.length > 0 && !searchValue" class="suggestion-section">
              <div class="suggestion-header">
                <HistoryOutlined />
                <span>搜索历史</span>
                <a-button type="link" size="small" @click="clearHistory">清空</a-button>
              </div>
              <div class="suggestion-tags">
                <a-tag
                  v-for="(item, index) in searchHistory"
                  :key="index"
                  class="suggestion-tag"
                  @click="selectSuggestion(item)"
                >
                  {{ item }}
                  <a-button
                    type="text"
                    size="small"
                    @click.stop="removeHistoryItem(index)"
                    class="remove-btn"
                  >
                    ×
                  </a-button>
                </a-tag>
              </div>
            </div>

            <!-- 热门搜索 -->
            <div v-if="!searchValue" class="suggestion-section">
              <div class="suggestion-header">
                <FireOutlined />
                <span>热门搜索</span>
              </div>
              <div class="suggestion-tags">
                <a-tag
                  v-for="item in hotSearches"
                  :key="item"
                  class="suggestion-tag hot-tag"
                  @click="selectSuggestion(item)"
                >
                  {{ item }}
                </a-tag>
              </div>
            </div>

            <!-- 搜索建议 -->
            <div v-if="searchSuggestions.length > 0" class="suggestion-section">
              <div class="suggestion-header">
                <SearchOutlined />
                <span>搜索建议</span>
              </div>
              <div class="suggestion-list">
                <div
                  v-for="suggestion in searchSuggestions"
                  :key="suggestion"
                  class="suggestion-item"
                  @click="selectSuggestion(suggestion)"
                >
                  <SearchOutlined />
                  <span>{{ suggestion }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

<!--    &lt;!&ndash; 分类导航 &ndash;&gt;
    <div class="category-nav">
      <div class="nav-container">
        <a-menu mode="horizontal" :selectedKeys="[activeCategory]" class="category-menu">
          <a-menu-item key="all" @click="setCategory('all')">全部</a-menu-item>
          <a-menu-item key="skincare" @click="setCategory('skincare')">护肤</a-menu-item>
          <a-menu-item key="makeup" @click="setCategory('makeup')">彩妆</a-menu-item>
          <a-menu-item key="perfume" @click="setCategory('perfume')">香水</a-menu-item>
          <a-menu-item key="tools" @click="setCategory('tools')">工具</a-menu-item>
          <a-menu-item key="bath" @click="setCategory('bath')">洗护</a-menu-item>
        </a-menu>
      </div>
    </div>-->

    <!-- 商品列表 -->
    <div class="product-section">
      <div class="section-header">
        <div class="header-left">
          <h2>✨ 精选商品</h2>
          <span class="product-count">共 {{ dataList.length }} 件商品</span>
        </div>
        <a-button type="link">查看更多 <RightOutlined /></a-button>
      </div>

      <!-- 无搜索结果提示 -->
      <div v-if="dataList.length === 0" class="no-results">
        <div class="no-results-content">
          <SearchOutlined class="no-results-icon" />
          <h3>暂无相关商品</h3>
          <p>试试其他关键词或分类</p>
          <a-button type="primary" @click="searchValue = ''; activeCategory = 'all'">
            查看全部商品
          </a-button>
        </div>
      </div>

      <!-- 商品网格 -->
      <div v-else class="product-grid">
        <div v-for="item in dataList" :key="item.id" class="product-item" @click="goToDetail(item)">
          <div class="product-image">
            <img :src="item.coverImg" :alt="item.title" />
            <div class="product-overlay">
              <a-button type="primary" size="small">立即购买</a-button>
            </div>
            <div class="product-badge" v-if="item.discount">
              {{ item.discount }}
            </div>
          </div>
          <div class="product-info">
            <h4 class="product-title">{{ item.title }}</h4>
            <p class="product-desc">{{ item.description }}</p>
            <div class="product-price">
              <span class="current-price">¥{{ item.discountedPrice }}</span>
              <span class="original-price">¥{{ item.price }}</span>
            </div>
            <div class="product-meta">
              <span class="sales">月销 {{ item.sales }}</span>
              <span class="shop">{{ item.storeName }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, computed, onMounted} from 'vue'
import { useRouter } from 'vue-router'
import { RightOutlined, SearchOutlined, FireOutlined, HistoryOutlined } from '@ant-design/icons-vue'

const router = useRouter()
const activeCategory = ref('all')
const searchValue = ref('')
const showSearchSuggestions = ref(false)
import pic1 from '@/assets/image/commodity1-1.png';
import pic2 from '@/assets/image/commodity1-2.png';
import {listProductsVoByPage} from "@/api/productController.ts";
import {message} from "ant-design-vue";
const searchParams = reactive<API.ProductQueryDTO>({
  current: 1,
  pageSize: 12,
});
const dataList = ref<API.ProductsVO[]>([]);
const total = ref(0);

//获取数据
const fetchData = async () => {
  const res = await listProductsVoByPage(searchParams);
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? [];
    total.value = res.data.data.total ?? 0;
  } else {
    message.error('获取数据失败: ' + res.data.msg);
  }
};

//自动加载
onMounted(() => {
  fetchData();
});


// 热门搜索词
const hotSearches = [
  '精华液', '面霜', '面膜', '洁面', '防晒', '口红', '粉底', '香水'
]

// 搜索历史
const searchHistory = ref(['兰蔻', '雅诗兰黛', 'SK-II'])



const goToDetail = (item: API.ProductsVO) => {
  router.push({
    path: `/product/${item.id}`,
  });
}
const handleSearch = (value: string) => {
  searchValue.value = value
  showSearchSuggestions.value = false

  // 添加到搜索历史
  if (value.trim() && !searchHistory.value.includes(value)) {
    searchHistory.value.unshift(value)
    if (searchHistory.value.length > 10) {
      searchHistory.value.pop()
    }
  }
}

const handleSearchFocus = () => {
  showSearchSuggestions.value = true
}

const handleSearchBlur = () => {
  setTimeout(() => {
    showSearchSuggestions.value = false
  }, 200)
}

const selectSuggestion = (suggestion: string) => {
  searchValue.value = suggestion
  showSearchSuggestions.value = false
  handleSearch(suggestion)
}

const clearHistory = () => {
  searchHistory.value = []
}

const removeHistoryItem = (index: number) => {
  searchHistory.value.splice(index, 1)
}
</script>


<style scoped>
.home-view {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: calc(100vh - 64px);
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 0;
}

/* 搜索区域样式 */
.search-section {
  background: white;
  padding: 24px 0;
  border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.search-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 0 24px;
  position: relative;
}

.search-box {
  position: relative;
}

.search-input {
  border-radius: 12px;
  overflow: hidden;
}

.search-input :deep(.ant-input) {
  border-radius: 12px 0 0 12px;
  border: 2px solid #e8e8e8;
  border-right: none;
  height: 48px;
  font-size: 16px;
  padding-left: 16px;
}

.search-input :deep(.ant-input-search-button) {
  border-radius: 0 12px 12px 0;
  height: 48px;
  width: 60px;
  background: #667eea;
  border-color: #667eea;
}

.search-input :deep(.ant-input-search-button:hover) {
  background: #5a6fd8;
  border-color: #5a6fd8;
}

.search-icon {
  color: #999;
  font-size: 16px;
}

/* 搜索建议样式 */
.search-suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  border: 1px solid #e8e8e8;
  z-index: 1000;
  margin-top: 8px;
  max-height: 400px;
  overflow-y: auto;
}

.suggestion-section {
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.suggestion-section:last-child {
  border-bottom: none;
}

.suggestion-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.suggestion-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.suggestion-tag {
  cursor: pointer;
  border-radius: 6px;
  padding: 4px 12px;
  font-size: 13px;
  transition: all 0.3s ease;
  position: relative;
}

.suggestion-tag:hover {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.hot-tag {
  background: #fff2f0;
  color: #ff4757;
  border-color: #ffccc7;
}

.hot-tag:hover {
  background: #ff4757;
  color: white;
  border-color: #ff4757;
}

.remove-btn {
  margin-left: 4px;
  padding: 0;
  width: 16px;
  height: 16px;
  line-height: 16px;
  font-size: 12px;
  color: #999;
}

.remove-btn:hover {
  color: #ff4757;
}

.suggestion-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.suggestion-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #666;
}

.suggestion-item:hover {
  background: #f5f5f5;
  color: #667eea;
}

.suggestion-item .anticon {
  font-size: 14px;
  color: #999;
}

/* 分类导航样式 */
.category-nav {
  display: flex;
  gap: 16px;
  padding: 16px 0 24px 0;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  background: white;
  border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  width: 100%;
  min-height: 60px;
  align-items: center;
}

.nav-container {
  width: 100%;
  padding: 0 24px;
  display: flex;
  align-items: center;
}

.category-menu {
  border-bottom: none;
  width: 100%;
  display: flex;
  align-items: center;
}

.category-menu :deep(.ant-menu-item) {
  height: 40px;
  line-height: 40px;
  margin: 0 8px 0 0;
  padding: 0 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.category-menu :deep(.ant-menu-item:hover) {
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
}

.category-menu :deep(.ant-menu-item-selected) {
  background: #667eea;
  color: white;
}

/* 商品区域样式 */
.product-section {
  background: white;
  border-radius: 0;
  padding: 24px 0 0 0;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.section-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.product-count {
  color: #666;
  font-size: 14px;
}

/* 无搜索结果样式 */
.no-results {
  padding: 60px 24px;
  text-align: center;
}

.no-results-content {
  max-width: 300px;
  margin: 0 auto;
}

.no-results-icon {
  font-size: 48px;
  color: #d9d9d9;
  margin-bottom: 16px;
}

.no-results h3 {
  margin: 0 0 8px 0;
  color: #666;
  font-size: 18px;
  font-weight: 500;
}

.no-results p {
  margin: 0 0 24px 0;
  color: #999;
  font-size: 14px;
}

/* 商品网格样式 */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
  width: 100%;
  padding: 0 24px 24px 24px;
  box-sizing: border-box;
}

.product-item {
  border: 1px solid #e8e8e8;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  background: white;
  position: relative;
}

.product-item:hover {
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  transform: translateY(-4px);
  border-color: #667eea;
}

.product-image {
  position: relative;
  height: 220px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-item:hover .product-image img {
  transform: scale(1.05);
}

.product-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.product-item:hover .product-overlay {
  opacity: 1;
}

.product-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: #ff4757;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

.product-info {
  padding: 16px;
}

.product-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
  line-height: 1.4;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-weight: 500;
}

.product-desc {
  margin: 0 0 12px 0;
  font-size: 12px;
  color: #999;
  height: 16px;
  overflow: hidden;
}

.product-price {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.current-price {
  font-size: 20px;
  font-weight: 700;
  color: #ff4757;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}

.sales {
  color: #ff6b6b;
  font-weight: 500;
}

.shop {
  color: #667eea;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .search-container {
    max-width: 100%;
    padding: 0 16px;
  }

  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    padding: 0 16px 16px 16px;
  }

  .category-nav {
    padding: 12px 0 20px 0;
    min-height: 50px;
  }

  .nav-container {
    padding: 0 16px;
  }

  .category-menu :deep(.ant-menu-item) {
    height: 36px;
    line-height: 36px;
    margin: 0 6px 0 0;
    padding: 0 12px;
    font-size: 13px;
  }

  .section-header {
    padding: 0 16px;
  }
}

@media (max-width: 480px) {
  .search-section {
    padding: 16px 0;
  }

  .search-container {
    padding: 0 12px;
  }

  .search-input :deep(.ant-input) {
    height: 44px;
    font-size: 14px;
  }

  .search-input :deep(.ant-input-search-button) {
    height: 44px;
  }

  .product-grid {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 0 12px 12px 12px;
  }

  .product-image {
    height: 120px;
  }

  .section-header {
    padding: 0 12px;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .header-left {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .category-nav {
    padding: 8px 0 16px 0;
    min-height: 45px;
  }

  .nav-container {
    padding: 0 12px;
  }

  .category-menu :deep(.ant-menu-item) {
    height: 32px;
    line-height: 32px;
    margin: 0 4px 0 0;
    padding: 0 8px;
    font-size: 12px;
  }

  .no-results {
    padding: 40px 12px;
  }
}
</style>
